package com.yu.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yu.bean.Picture;
import com.yu.bean.PictureExample;
import com.yu.bean.AdminExample.Criteria;
import com.yu.dao.PictureMapper;


@Service
public class PictureService {
	
	@Autowired
	PictureMapper pictureMapper;

	
	/**
	 * 上传图片的地址插入到数据库中
	 * @param id 
	 * @param storUrl 
	 * 
	 */
	public void uploadImgs(String id, String storUrl) {
		Picture p =new Picture();
		p.setInfoId(Integer.parseInt(id));
		p.setPicUrl(storUrl);
		pictureMapper.insert(p);
	}


	public List<Picture> getPictureById(String infoId) {
		PictureExample pExample = new PictureExample();
		com.yu.bean.PictureExample.Criteria criteria =pExample.createCriteria();
		criteria.andInfoIdEqualTo(Integer.parseInt(infoId));
		List<Picture> pics = new ArrayList<>();
		pics =pictureMapper.selectByExample(pExample);
		// TODO Auto-generated method stub
		return pics;
	}


	/**
	 * 上传多个内容图片成功后全部返回给页面
	 * @param storUrl
	 * @return
	 */
	public Picture selectPicByUrl(String storUrl) {
		// TODO Auto-generated method stub
		PictureExample pictureExample = new PictureExample();
		com.yu.bean.PictureExample.Criteria criteria = pictureExample.createCriteria();
		criteria.andPicUrlEqualTo(storUrl);
		List<Picture> pics = pictureMapper.selectByExample(pictureExample);
		if(pics.size() != 0)
			return pics.get(0);
		else
			return null;
		
	}


	/**
	 * 根据 内容图片id删除图片
	 * @param dId
	 */
	public void delPicById(Integer dId) {
		// TODO Auto-generated method stub
		pictureMapper.deleteByPrimaryKey(dId);
		
	}


	/**
	 * 批量删除数据图片
	 * @param delIds
	 */
	public  void delPicsById(String delIds) {
		// TODO Auto-generated method stub
		String [] ids = delIds.split("-");
		for (String s : ids) {
			Integer dId = Integer.parseInt(s);
			pictureMapper.deleteByPrimaryKey(dId);
		}
		
	}


	/**
	 * 根据批量删除的infoIds 批量的删除
	 * @param del_ids
	 */
	public void deleteConPicsByInfoIds(List<Integer> del_ids) {
		// TODO Auto-generated method stub
		PictureExample pictureExample = new PictureExample();
		com.yu.bean.PictureExample.Criteria criteria = pictureExample.createCriteria();
		criteria.andInfoIdIn(del_ids);
		pictureMapper.deleteByExample(pictureExample);
		
	}


	/**
	 * 根据要删除的信息删除图片内容id
	 * @param id
	 */
	public void deletConPicByInfoId(Integer id) {
		// TODO Auto-generated method stub
		PictureExample pictureExample = new PictureExample();
		com.yu.bean.PictureExample.Criteria criteria = pictureExample.createCriteria();
		criteria.andInfoIdEqualTo(id);
		pictureMapper.deleteByExample(pictureExample);
		
	}
	

}
